//二次封装axios

import axios from 'axios'

//实例化axios
var service = axios.create({
    timeout: 10 * 1000,
    baseURL: '/api'
});

//设置请求拦截器
service.interceptors.request.use((config) => {
    //统一设置请求头
    config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
    return config;
}, (error) => {
    return Promise.reject(error)
})

//设置响应拦截器
service.interceptors.response.use((res) => {
    return res;
}, (error) => {
    if (error.response.status == 401) {
        Toast.fail('身份认证失败,请重新登录');
    } else if (error.response.status == 400) {
        Toast.fail('未找到资源');
    } else if (error.response.status == 500) {
        Toast.fail('服务器内部出现问题');
    }

    return Promise.reject(error);
})

//抛出
export default service;